package scau.erps.view.freemarket;	

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import scau.erps.app.Erps;
import scau.erps.entity.FreeMarket;
import scau.erps.service.FreeMarketService;
import scau.erps.service.ResearchService;
import scau.erps.service.impl.FreeMarketServiceImpl;
import scau.erps.service.impl.ResearchServiceImpl;
import scau.erps.util.FormatValidation;



/**
 * 自由市场发布对话框
 * @author outsider
 * 
 */
public class addDialog extends JDialog implements ActionListener{
	private static final long serialVersionUID = 1L;
	private JTextField numJtf,priceJtf;
	private JComboBox<String> materialCombo;
	private JComboBox<String> productCombo;
	private JComboBox<String> researchCombo;
	private String total,price,product;
	private JButton generateBtn;
	private String type;
	public addDialog(String type){
		this.setTitle("发布订单");
		this.setSize(350,215);
		this.type=type;
		JPanel contentPanel = new JPanel();
		contentPanel.setLayout(new BorderLayout());	
		JPanel panel1 = new JPanel();
		
		if (type.equals("Material")){
			JLabel firstLable=new JLabel("原材料种类");
			materialCombo = new JComboBox<String>(new String[]{"M1","M2","M3","M4"});
			panel1.add(firstLable);
			panel1.add(materialCombo);
		}
		
		if (type.equals("Product")){
			JLabel firstLable=new JLabel("产品种类");
			productCombo = new JComboBox<String>(new String[]{"Beryl","Crystal","Ruby","Sapphire"});
			panel1.add(firstLable);
			panel1.add(productCombo);
			
		}
		
		if (type.equals("Research")){
			JLabel firstLable=new JLabel("研发技术");
			researchCombo=new JComboBox<String>();
			ResearchService researchService=Erps.getIocInstance().get(ResearchServiceImpl.class);
			if (researchService.getResearchStatus(Erps.getCurrentTeam().getId(),"Crystal"))
				researchCombo.addItem("Crystal");
			if (researchService.getResearchStatus(Erps.getCurrentTeam().getId(),"Ruby"))
				researchCombo.addItem("Ruby");
			if (researchService.getResearchStatus(Erps.getCurrentTeam().getId(),"Sapphire"))
				researchCombo.addItem("Sapphire");
			panel1.add(firstLable);
			panel1.add(researchCombo);
			if (researchCombo.getItemCount()==0){
				JOptionPane.showMessageDialog(this, "当前没有可出售技术", "提示", JOptionPane.INFORMATION_MESSAGE);
				return;
			}
		}
		
		JPanel panel2 = new JPanel();
		JLabel priceLabel = new JLabel("单价:");
		priceJtf = new JTextField(4);	
		priceJtf.setText("1");
		JPanel panel5 = new JPanel();
		panel5.add(priceLabel);
		panel5.add(priceJtf);		
		
		JPanel panel3 = new JPanel();
		JLabel numLabel = new JLabel("数量:");
		numJtf = new JTextField(4);
		numJtf.setText("0");
		panel3.add(numLabel);
		panel3.add(numJtf);
		
		
		if (type.equals("Material")) {
			panel2.add(panel3);
			//panel2.add(panel5);
		}
		if(type.equals("Product")) panel2.add(panel5);
		if(type.equals("Research")) panel2.add(panel5);
		
		JPanel panel4 = new JPanel();
		generateBtn = new JButton("确认");
		generateBtn.addActionListener(this);		
		panel4.add(generateBtn);
		
	
		contentPanel.add(panel1,BorderLayout.NORTH);
		contentPanel.add(panel2,BorderLayout.CENTER);
		contentPanel.add(panel4,BorderLayout.SOUTH);
		this.setContentPane(contentPanel);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		this.setResizable(false);
		this.setModal(true);
		this.setVisible(true);
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==generateBtn) {				
             FreeMarketService freeMarketService=Erps.getIocInstance().get(FreeMarketServiceImpl.class);
             FreeMarket freeMarket=new FreeMarket();
             int cost=0,price=0;
             String product=null;
             if (FormatValidation.isNumeric(numJtf.getText())==false){
            	 JOptionPane.showMessageDialog(null, "数量请输入整数", "提示",JOptionPane.INFORMATION_MESSAGE);
            	 return;
             }
             if (FormatValidation.isNumeric(priceJtf.getText())==false){
            	 JOptionPane.showMessageDialog(null, "价格请输入整数", "提示",JOptionPane.INFORMATION_MESSAGE);
            	 return;
             }
             freeMarket.setTeamId(Erps.getCurrentTeam().getId());
             freeMarket.setType(type);
             if (type.equals("Material")){
            	 product=materialCombo.getSelectedItem().toString();
            	 freeMarket.setProduct(product);
            	 freeMarket.setNum(Integer.parseInt(numJtf.getText()));
             }
             if (type.equals("Product")){
            	 product=productCombo.getSelectedItem().toString();
            	 freeMarket.setProduct(product);
            	 freeMarket.setNum(1);
             }
             if (type.equals("Research")){
            	 product=researchCombo.getSelectedItem().toString();
            	 freeMarket.setProduct(product);
            	 freeMarket.setNum(1);
             }
             price=Integer.parseInt(priceJtf.getText())*freeMarket.getNum();  //计算总价
             if(type.equals("Material")){
            	 freeMarket.setPrice(price/2);
             }else{
            	 freeMarket.setPrice(price);
             }
             
             if (freeMarket.getNum()<=0||freeMarket.getPrice()<=0){
            	 JOptionPane.showMessageDialog(null, "请输入大于零的数字", "提示",JOptionPane.INFORMATION_MESSAGE);
            	 return;
             }   
             
             
             int flag=freeMarketService.add(freeMarket);
				if (flag==0){
					JOptionPane.showMessageDialog(this, "发布成功", "提示", JOptionPane.INFORMATION_MESSAGE);
					dispose();
				}
				if (flag==-1){
					JOptionPane.showMessageDialog(this, "所要出售的原材料不足", "提示", JOptionPane.INFORMATION_MESSAGE);
				}
				if (flag==-2){
					JOptionPane.showMessageDialog(this, "出售单价不符合规则，出售单价需大于成本价*1.5，小于成本价*3", "提示", JOptionPane.INFORMATION_MESSAGE);
				}
				if (flag==-5){
					JOptionPane.showMessageDialog(this, "所有出售的成品不足", "提示", JOptionPane.INFORMATION_MESSAGE);
				} 
				if(flag==-6){
					JOptionPane.showMessageDialog(this, "出售原材料数量为2的倍数", "提示", JOptionPane.INFORMATION_MESSAGE);
				}

		}		
	}
	


}